
package nl.hanze.shoestring.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import nl.hanze.shoestring.domain.Continent;
import nl.hanze.shoestring.logic.Logic;

/** 
 * Beheer Continent tabel
 * 
 * @author bleker
 */

public class ContinentPanel extends JPanel{
    
        Logic logic;
        JPanel pQueryInput, pRecord, pNavigation, pDbGrid;
        JLabel lblZoekSleutel, lblId, lblContinentNaam;
        JTextField txtZoekSleutel, txtId, txtContinentNaam;
        JButton butZoeken, butSave, butDelete, butEmpty;
        JTable table;
        JScrollPane scrollPaneRes;
        DefaultTableModel model;
        Continent continentObj=null;
        List<Continent> continentList;


        int heightField = 25;
        int numberOfRows = 22;

        public ContinentPanel(Logic logic) {
            this.logic = logic;
 
        table = new JTable();
        setLayout(new BorderLayout(20, 20));

        // input velden north sectie
//        pQueryInput = new JPanel();
//        pQueryInput.setLayout(null);
//        pQueryInput.setPreferredSize(new Dimension(400, 60));
//
//        lblZoekSleutel = UIHelper.createLabel(lblZoekSleutel = new JLabel(), "Zoeksleutel", 10, 10, 150, heightField, pQueryInput);
//        txtZoekSleutel = UIHelper.createTextField(txtZoekSleutel = new JTextField(), "", 170, 10, 250, heightField, pQueryInput);
//
//        butZoeken = UIHelper.createButton(butZoeken = new JButton(), "Zoeken", 430, 10, 100, heightField, pQueryInput);
//        butZoeken.addActionListener(
//                new ActionListener() {
//                    @Override
//                    public void actionPerformed(ActionEvent e) {
//                        System.out.println("zoek");
//                    }
//                });

        // Center sectie, database record details
        pRecord = new JPanel();
        pRecord.setLayout(null);
        pRecord.setPreferredSize(new Dimension(400, 500));
        pRecord.setBorder(BorderFactory.createLineBorder(Color.blue));
        
        lblId = UIHelper.createLabel(lblId = new JLabel(), "Nummer", 20, 10, 150, heightField, pRecord);
        txtId = UIHelper.createTextField(txtId = new JTextField(), "", 170, 10, 120, heightField, pRecord);

        lblContinentNaam = UIHelper.createLabel(lblContinentNaam = new JLabel(), "ContinentNaam", 20, 70, 150, heightField, pRecord);
        txtContinentNaam = UIHelper.createTextField(txtContinentNaam = new JTextField(), "", 170, 70, 300, heightField, pRecord);

        butSave = UIHelper.createButton(butSave = new JButton(), "Opslaan", 170, 150, 120, heightField, pRecord);
        butSave.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("save");
                        if(continentObj!=null) {
                            updateRecord(continentObj);
                        } else {
                            addRecord();
                        }
                    }
                });


        butDelete = UIHelper.createButton(butDelete = new JButton(), "Verwijderen", 350, 150, 120, heightField, pRecord);
        butDelete.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("delete");
                        if(continentObj!=null) {
                            deleteRecord(continentObj);
                        }
                }});
        butDelete.setVisible(false);
        
        butEmpty = UIHelper.createButton(butDelete = new JButton(), "Leeg/Nieuw", 350, 10, 120, heightField, pRecord);
        butEmpty.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        emptyAll();
                }});
        
        // fill tab pane
        add(pRecord, BorderLayout.CENTER);
//        add(pQueryInput, BorderLayout.NORTH);
        addTable();
        emptyAll();
    }

    /**
     * Haal Continent informatie uit database en toon in JTable
     */
    private void addTable() {

        // Gridcontainer
        pDbGrid = new JPanel(new BorderLayout(2, 3));

        // scroll pane eigenschappen
        scrollPaneRes = new JScrollPane(table);
        Dimension d = table.getPreferredSize();
        scrollPaneRes.setPreferredSize(new Dimension(d.width, table.getRowHeight() * numberOfRows));

        // navigatie container
        UIHelper.addNavigation(pNavigation = new JPanel(), table, scrollPaneRes, numberOfRows);
        
        // kolom eigenschappen - breedte
        Object[] headers = {"Nummer", "Continent"};
        int[] breedte = {100, 300};
        
        // tablemodel en eigenschappen instellen, kolombreedte etc
        model = new DefaultTableModel(headers, 0) {

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
            
            // rendering van cellen, toont checkbox en juiste datum indien datatype overeenkomstig is
            // null velden veroorzaken fout, standaard maar op String gezet....
            @Override
            public Class getColumnClass(int column){
                try {
                 return getValueAt(0, column).getClass();
                } catch (java.lang.NullPointerException e) {
                    return String.class;
                }
            }

        };

        table.setModel(model);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        for (int i0 = 0; i0 < breedte.length; i0++) {
            table.getColumnModel().getColumn(i0).setPreferredWidth(breedte[i0]);
        }
        
        updateTable(true);
        
        // selecteer row door muisklik    
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                continentObj = continentList.get(table.convertRowIndexToModel(table.getSelectedRow()));
                getRecord(continentObj);
            }
        });

        pDbGrid.add(scrollPaneRes, BorderLayout.CENTER);
        pDbGrid.add(pNavigation, BorderLayout.SOUTH);
        add(pDbGrid, BorderLayout.SOUTH);
    }
    
    
    /*
     *  Vul detailvelden met geselecteerde rowdata  
     */
    private void getRecord(Continent continentObj) {
        txtId.setText("" + continentObj.getId());
        txtId.setEditable(false);
        txtContinentNaam.setText("" + continentObj.getNaam());
    }
    
    /*
     *  update record  
     */
    private void updateRecord(Continent continentObj) {
        continentObj.setNaam(txtContinentNaam.getText());
        logic.updateContinent(continentObj);
        updateTable(true);
        emptyAll();
    }
    
    /*
     *  add new record  
     */
    private void addRecord() {
        continentObj = new Continent();
        continentObj.setNaam(txtContinentNaam.getText());
        logic.addContinent(continentObj);
        updateTable(true);
        emptyAll();
    }

    /*
     *  delete record  
     */
    private void deleteRecord(Continent continentObj) {
        logic.deleteContinent(continentObj);
        updateTable(true);
        emptyAll();
    }
    
    /*
     *  maak velden leeg en continentObj =null  
     */
    private void emptyAll() {
        txtId.setEditable(false);
        txtId.setText("");
        txtContinentNaam.setText("");
        
        continentObj=null;
    }
    
    private void updateTable(Boolean refresh) {
        // data in JTable plaatsen
        if(refresh) {
            continentList = logic.getContinentList();
        }
        model.setRowCount(0);
        // data in JTable plaatsen
        for (Continent continent : continentList) {
            Object[] o = {continent.getId(),
                continent.getNaam()};
            model.addRow(o);
        }
    }
    
}

    

